package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.TreeNode;

public class Solution_0606 {
    /**
     * 根据二叉树创建地址
     *
     * @param root
     * @return
     */
    public String tree2str(TreeNode root) {
        this.dfs(root);
        return stringBuffer.toString().substring(1, stringBuffer.toString().length()-1);
    }

    public StringBuffer stringBuffer = new StringBuffer();

    public void dfs(TreeNode root) {
        if (root == null) {
            stringBuffer.append("()");
            return;
        }
        stringBuffer.append("(");
        stringBuffer.append(root.val);
        if (root.left == null && root.right != null) {
            dfs(root.left);
            dfs(root.right);
        } else if (root.left != null && root.right != null) {
            dfs(root.left);
            dfs(root.right);
        } else if (root.left != null && root.right == null) {
            dfs(root.left);
        } else {

        }

        stringBuffer.append(")");
    }
}
